<script setup lang="ts">
//官方模块
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
//自定义方法
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
//组件
import CustomNavbar from './components/CustomNavbar'
import CategoryPanel from './components/CategoryPanel'
import HotPanel from './components/HotPanel'
//ts类型
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
//骨架屏
import PageSkeleton from './components/PageSkeleton'
//下滑加载更多
import { useGuessList } from '@/composables/index'
//存储轮播图的数组
const bannerList = ref<BannerItem[]>()
/**
 * 获取轮播图
 */
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)
  bannerList.value = res.result
}
//存储分类信息的数组
const categoryList = ref<CategoryItem[]>()
/**
 * 获取分类数据
 */
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
  // console.log(res);
}
const hotList = ref<HotItem>()
/**
 * 获取特惠推荐的数据
 */
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
  // console.log(res)
}
//骨架屏的显示控制
const idLoading = ref<boolean>(true)
//页面加载发送请求
onLoad(async () => {
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  idLoading.value = false
})
//触底加载更多
const { guessRef, onScrolltolower } = useGuessList()

//下拉刷新动画控制
const isTriggered = ref<boolean>(false)
//下拉刷新
const onRefresherrefresh = async () => {
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  isTriggered.value = false
}
</script>

<template>
  <CustomNavbar />
  <scroll-view
    refresher-enabled="true"
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    class="scroll-view"
    scroll-y
    @scrolltolower="onScrolltolower"
  >
    <PageSkeleton v-if="idLoading" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanel :list="categoryList" />
      <HotPanel :list="hotList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
// #ifdef APP-PLUS
#app
// #endif
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
  .scroll-view {
    flex: 1;
  }
}
</style>
